<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/27 0027
 * Time: 10:40
 */

namespace Cohuna\Payment;

include_once __DIR__ . '/Alipay/AlipaySdk.php';

class PaymentFactory
{

    public static function createPayment(array $config,array $biz_content)
    {
        switch ($config['trade_channel']){
            case 'alipay_pc_direct':{
                $alipay_config = PaymentFactory::convertAlipayConfig($config);
                //如果是
                if($config['gateway'] === 'partner'){
                    $service = new \AlipayTradeService($alipay_config);
                    $builder = new \CreateDirectPayByUserBuilder();

                    $builder->setOutTradeNo($biz_content['order_no']);
                    $builder->setSubject($biz_content['order_subject']);
                    $builder->setTotalFee($biz_content['total_amount']);
                    $builder->setSellerEmail($alipay_config['seller_email']);
                    $builder->setSellerId($alipay_config['partner']);

                    if(isset($biz_content['time_expire']) && isset($biz_content['time_start'])){
                        $builder->setItBPay(static::resolveAlipayExpire($biz_content['time_start'],$biz_content['time_expire']));
                    }else{
                        $builder->setItBPay('48h');
                    }
                    if(isset($biz_content['body'])){
                        $builder->setBody($biz_content['body']);
                    }
                    if(isset($biz_content['attach'])){
                        $builder->setExtraCommonParam($biz_content['attach']);
                    }
                    if(isset($biz_content['return_url'])){
                        $builder->setReturnUrl($biz_content['return_url']);
                    }
                    if(isset($biz_content['notify_url'])){
                        $builder->setNotifyUrl($biz_content['notify_url']);
                    }

                    $response = $service->pageExecute($builder,'get');
                    return $response;
                }
                break;
            }

        }
    }

    /**
     * 将配置转换为支付宝SDK配置
     * @param array $config
     * @return array
     */
    public static function convertAlipayConfig(array $config)
    {
        $alipay_config = array();

        $alipay_config['app_id']            = isset($config['app_id']) ? $config['app_id'] : null;
        $alipay_config['seller_email']      = isset($config['alipay_account']) ? $config['alipay_account'] : null;
        $alipay_config['key']               = isset($config['encryption_key']) ? $config['encryption_key'] : null;
        $alipay_config['input_charset']     = isset($config['input_charset']) ? $config['input_charset'] : 'utf-8';
        $alipay_config['cacert_path']       = isset($config['cacert']) ? $config['cacert'] : __DIR__ . '/Alipay/cert/cacert.pem';
        $alipay_config['rsa_private_cert']  = isset($config['alipay_private_cert']) ? $config['alipay_private_cert'] : null;
        $alipay_config['rsa_public_cert']   = isset($config['alipay_public_cert']) ? $config['alipay_public_cert'] : null;
        $alipay_config['partner']           = isset($config['partner']) ? $config['partner'] : null;

        switch ($config['sign_type']){
            case 'md5' : $alipay_config['sign_type'] = 'MD5';break;
            case 'SHA1WithRSA' : $alipay_config['sign_type'] = 'RSA';break;
            case 'SHA256WithRSA': $alipay_config['sign_type'] = 'RSA2';break;
            case 'AES': $alipay_config['sign_type'] = 'AES';break;
            case 'DSA': $alipay_config['sign_type'] = 'DSA';break;
        }

        return $alipay_config;
    }

    /***
     * 将两个日期转换为支付宝可识别的过期时间
     * @param string $start_time
     * @param string $end_time
     * @return string
     */
    public static function resolveAlipayExpire($start_time,$end_time)
    {
        $minute = intval((strtotime($end_time)-strtotime($start_time))%86400/60);
        return $minute . 'm';
    }

}